<?php

namespace App\Http\Controllers\Logic;

use App\Http\Controllers\Controller;
use App\Model\OACompany;
use App\Model\OADept;
use App\Model\OAStaff;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class CompanyLogic extends Controller
{

    protected $status_list = ['禁用','正常'];
    //获取公司信息
    public static function getCompanys()
    {
        $model = new OACompany();
        $currency = config('oa.currency');
        return ['companys'=>$model->select(['fhs_gs_id as gsid','fhs_gs_name as gsname'])->get()->toArray(),'currency'=>$currency];
    }
    //通过公司查对应的部门
    public static function getDepts($input)
    {
        try{
        $gsid = $input['gsid'];
        //查公司所在部门
            $bmids = OACompany::query()->where('id',$gsid)
                                        ->select('dept_id')
                                        ->first()
                                        ->toArray();
            $query = OADept::query();
            $dept_ids = $bmids['dept_id'];
            $depts = $query->whereRaw(DB::Raw("FIND_IN_SET (id,'{$dept_ids}')"))
                                    ->where('state',1)
                                    ->select(['id','dept_name'])
                                    ->get()
                                    ->toArray();
            return $depts;

        }catch (\Exception $e){
           throw new \Exception($e->getMessage() . __METHOD__ . ' on line ' . __LINE__);
        }


    }

    //获取公司列表
    public function getList($params)
    {
        $query = OACompany::query();

        if($short_name = $params['short_name']){
            $query->where('short_name','like','%' . $short_name .'%');
        }

        if($name = $params['name']){
            $query->where('name','like','%' . $name .'%');
        }

        if($status = $params['status']){
            $query->where('status',$status);
        }

        $query->orderBy('updated_at','DESC');

        $list = $query->paginate($params['page_size'],['*'],'page')->toArray();
        //dd($list);
        if($list['data']){
            foreach ($list['data'] as &$val){
                $dept_arr = explode(',',$val['deptid']);
                $val['deptid'] = array_map(function($val){return intval($val);},$dept_arr);
            }
        }

        $list['status_list'] = $this->status_list;
        //获取职位下拉列表
        $position_list = $this->getStaffList();

        return array_merge($position_list,$list);
    }

    //获取职位下拉
    protected function getStaffList()
    {
        $list = OAStaff::query()->where('status',1)->select(['position_id','id','username','roleid'])->get()->toArray();
        $response = [];
        foreach ($list as $item){
            if(Str::contains($item['roleid'],'218')) $response['cw_list'][] = $item;
            if(Str::contains($item['roleid'],'227')) $response['cg_list'][] = $item;
            if(Str::contains($item['roleid'],'212')) $response['cgzj_list'][] = $item;
            if(Str::contains($item['roleid'],'211')) $response['fzjl_list'][] = $item;
            if(Str::contains($item['roleid'],'210')) $response['zjl_list'][] = $item;
            if(Str::contains($item['roleid'],'219')) $response['xz_list'][] = $item;
            if(Str::contains($item['roleid'],'225')) $response['cn_list'][] = $item;
            if(Str::contains($item['roleid'],'226')) $response['sj_list'][] = $item;
            if(Str::contains($item['roleid'],'214')) $response['sybid_list'][] = $item;
            unset($item['roleid']);
        }

        $data = (new CommonLogic())->getCommonData(['dept']);
        $response['dept_list'] = $data['dept'];
        return $response;
    }

    //编辑公司
    public function editCompany($params)
    {
        DB::beginTransaction();
        try{
            $companyInfo = OACompany::query()->where('id',$params['id'])->first();
            $companyInfo->short_name = $params['short_name'];
            $companyInfo->name = $params['name'];
            $companyInfo->tax = $params['tax'];
            $companyInfo->status = $params['status'];
            $companyInfo->deptid = implode(',',$params['deptid']);
            $companyInfo->cwid = $params['cwid'];
            $companyInfo->cgid = $params['cgid'];
            $companyInfo->cnid = $params['cnid'];
            $companyInfo->xzid = $params['xzid'];
            $companyInfo->sjid = $params['sjid'];
            $companyInfo->fzid = $params['fzid'];
            $companyInfo->zjlid = $params['zjlid'];
            $companyInfo->cgjlid = $params['cgjlid'];
            $companyInfo->syzjid = $params['syzjid'];
            $companyInfo->save();
            DB::commit();
            return true;
        }catch (\Exception $e){
            DB::rollBack();
            throw new \Exception($e->getMessage());
        }
    }

    //添加公司
    public function addCompany($params)
    {
        try {
            DB::beginTransaction();
            $isExists = OACompany::query()->where('short_name',$params['short_name'])->exists();
            if($isExists) throw new \Exception('公司简写'.$params['short_name'].'已存在');
            $companyInfo['short_name'] = $params['short_name'];
            $companyInfo['name'] = $params['name'];
            $companyInfo['tax'] = $params['tax'];
            $companyInfo['deptid'] = implode(',', $params['deptid']);
            $companyInfo['cwid'] = $params['cwid'];
            $companyInfo['cgid'] = $params['cgid'];
            $companyInfo['cnid'] = $params['cnid'];
            $companyInfo['xzid'] = $params['xzid'];
            $companyInfo['sjid'] = $params['sjid'];
            $companyInfo['fzid'] = $params['fzid'];
            $companyInfo['zjlid'] = $params['zjlid'];
            $companyInfo['cgjlid'] = $params['cgjlid'];
            $companyInfo['syzjid'] = $params['syzjid'];
            $companyInfo['status'] = 1;
            $companyInfo['updated_at'] = date('Y-m-d H:i:s');
            $insertId = OACompany::query()->insertGetId($companyInfo);
            DB::commit();
            return $insertId;
        }catch (\Exception $e){
            DB::rollBack();
            throw new \Exception($e->getMessage());
        }

    }

}
